//
//  GZConsoleLogger.m
//  GZLogger
//
//  Created by GR on 2018/12/20.
//  Copyright © 2018 GR. All rights reserved.
//

#import "GZConsoleLogger.h"
#import "GZConsoleLogFormatter.h"
#import "GZLogItem.h"

@implementation GZConsoleLogger

- (instancetype)init {
    if (self = [super init]) {
        self.formatter = [[GZConsoleLogFormatter alloc] init];
    }
    return self;
}

#pragma mark - Private
- (BOOL)shouldRecordItem:(GZLogItem *)item {
    
    NSNumber *levelValue = @(item.level);
    
    if (self.fliterLevels.count &&
        ![self.fliterLevels containsObject:levelValue]) {
        return NO;
    }
    
    if (self.fliterTags.count &&
        ![self.fliterTags containsObject:item.tag]) {
        return NO;
    }
    
    return YES;
}


- (void)recordLogItem:(GZLogItem *)item {
    
    NSParameterAssert(self.formatter);
    
    if (![self shouldRecordItem:item]) {
        return;
    }

    NSString *formattedText = [self.formatter formattedTextWithItem:item];
    
    fprintf(stderr, "%s", formattedText.UTF8String);
}

@end
